package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Customer;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.query.CustomerQuery;
import com.by.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Api(tags = "客户管理")
@RequestMapping("/v1")
@RestController
public class CustomerController extends BaseController {
    @Autowired
    CustomerService customerService;

    @ApiOperation("修改客户")
    @PutMapping("/customer")
    public ResultBean update(@Validated @RequestBody Customer customer, BindingResult result) {
        validate(result);
        int rs = customerService.update(customer);
        return updateResult(rs);
    }

    @ApiOperation("查询客户")
    @GetMapping("/customer")
    public ResultBean query(int id) {
        Customer customer = customerService.get(id);
        return success("customer", customer);
    }
    @ApiOperation("客户列表")
    @PostMapping("/customer/query")
    public ResultBean queryAll(@RequestBody CustomerQuery query) {
        Map map = BeanUtil.beanToMap(query);
        PageBean bean = customerService.query(map);
        return success("客户列表", bean);
    }

    @ApiOperation("新增客户")
    @PostMapping("/customer")
    public ResultBean save(@Validated(ISave.class) @RequestBody Customer customer) {
        int i = customerService.save(customer);
        return saveResult(i);
    }

}
